package net.huashitong.synthetical.question.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import net.huashitong.synthetical.question.entity.SupervisionTask;
import net.huashitong.synthetical.question.filter.SupervisionTaskFilter;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.stereotype.Repository;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class SupervisionTaskDao extends StringDao<SupervisionTask> {


    /**
     * 任务清单页面查询主入口
     * @param filter
     * @return
     */
    public List<SupervisionTask> getMissinTask(SupervisionTaskFilter filter)
    {
        GenericQuery query = createQuery( "obj");
        if (filter.getTasktimes()!=null&&!"".equals(filter.getTasktimes()))
            query.append(" and taskTimes =:taskTimes").setParam("taskTimes",filter.getTasktimes());
        if (filter.getTaskName()!=null&&!"".equals(filter.getTaskName()))
            query.append(" and taskName =:taskName").setParam("taskName",filter.getTaskName());
        if (filter.getProjectName()!=null&&!"".equals(filter.getProjectName()))
            query.append(" and projectName =:projectName").setParam("projectName",filter.getProjectName());
        if (filter.getTaskContent()!=null&&!"".equals(filter.getTaskContent()))
            query.append(" and taskContent =:taskContent").setParam("taskContent",filter.getTaskContent());
        if (filter.getStartDate()!=null&&!"".equals(filter.getStartDate()))
            query.append(" and startDate =:startDate").setParam("startDate",filter.getStartDate());
        if (filter.getEndDate()!=null&&!"".equals(filter.getEndDate()))
            query.append(" and endDate =:endDate").setParam("endDate",filter.getEndDate());
        if (filter.getRespResult()!=null&&!"".equals(filter.getRespResult()))
            query.append(" and respResult =:respResult").setParam("respResult",filter.getRespResult());
        if (filter.getSupervisionResult()!=null&&!"".equals(filter.getSupervisionResult()))
            query.append(" and supervisionResult =:supervisionResult").setParam("supervisionResult",filter.getSupervisionResult());
        if (filter.getNodeId()!=null&&!"".equals(filter.getNodeId()))
            query.append(" and nodeId =:nodeId").setParam("nodeId",filter.getNodeId());
        if (filter.getResDept()!=null&&!"".equals(filter.getResDept()))
            query.append(" and resDept =:resDept").setParam("resDept",filter.getResDept());
        if (filter.getResUser()!=null&&!"".equals(filter.getResUser()))
            query.append(" and resUser =:resUser").setParam("resUser",filter.getResUser());
        if (filter.getOperatorUser()!=null&&!"".equals(filter.getOperatorUser()))
            query.append(" and operatorUser =:operatorUser").setParam("operatorUser",filter.getOperatorUser());
        return query.listResult(filter);
    }

    public void savetask()
    {
        ResultSet set  = null;
        try {
            Connection conn  = SessionFactoryUtils.getDataSource(sessionFactory).getConnection();
            String call = "{call tasktest() }";
            CallableStatement cs = conn.prepareCall(call);
            cs.execute();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
